package org.hpin.asset.web;

import java.util.Date;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.AssetDamageInfo;
import org.hpin.asset.entity.Assetinfo;
import org.hpin.asset.entity.Assetmaintaininfo;
import org.hpin.asset.service.AssetDamageInfoService;
import org.hpin.asset.service.AssetinfoService;
import org.hpin.asset.util.Duuid;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;


@Namespace("/assetDamange")
@Action("assetDamangeAction")
@Results({
	@Result(name="listAssetDinfo" ,location = "/WEB-INF/assetDamange/listAssetDinfo.jsp"),
	@Result(name="addAssetDinfo" ,location = "/WEB-INF/assetDamange/addAssetDinfo.jsp"),
	@Result(name="browAssetDinfo", location = "/WEB-INF/assetDamange/browAssetDinfo.jsp"),
	@Result(name="modifyAssetDinfo", location = "/WEB-INF/assetDamange/modifyAssetDinfo.jsp")
	})
public class AssetDamageInfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8311689233239083308L;
	private AssetDamageInfo assetDinfo;
	private Assetinfo assetinfo;
	private AssetDamageInfoService assetDinfoService=(AssetDamageInfoService) SpringTool.getBean(AssetDamageInfoService.class);
	private AssetinfoService assetinfoService=(AssetinfoService) SpringTool.getBean(AssetinfoService.class);
	private AssetinfoAction assetinfoAction =new AssetinfoAction();
	
	/**
	 * 资产报损查询
	 * @return
	 */
	public String listAssetDinfo() throws Exception{
		page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
		Map searchMap = super.buildSearch();
		findByPage(page, searchMap);
		return "listAssetDinfo";
	}
	public Page findByPage(Page page , Map searchMap){
		assetDinfoService.findByPage(page, searchMap);
		return page ;
	}
	
	/**
	 * 查询资产报损详细信息
	 * @return
	 */
	public String browAssetDinfo(){
		assetDinfo=(AssetDamageInfo) assetDinfoService.findById(id);
		String id=assetDinfo.getAssetno();
		assetinfo=(Assetinfo) assetinfoService.findById(id);
		return "browAssetDinfo";
	}
	
	public String addAssetDinfo() throws Exception{
		HttpTool.setAttribute("navTabId", HttpTool.getParameter("navTabId"));
		
		if(StrUtils.isNotNullOrBlank(id)){
			assetDinfo=(AssetDamageInfo) assetDinfoService.findById(id);
			HttpTool.setAttribute("id", id);
		}else{
			assetDinfo = new AssetDamageInfo();
			page = new Page(HttpTool.getPageNum(),HttpTool.getPageSize());
			Map searchMap = super.buildSearch();
			searchMap.put("filter_and_state_NEQ_S", "1010104");
			searchMap.put("filter_and_isDeleted_EQ_I",0);
			assetinfoAction.findByPage(page, searchMap);
		}
		return "addAssetDinfo";
	}
	
	//添加资产报损信息保存到数据库
		public String saveAssetDinfo(){
			JSONObject json = new JSONObject();
			try {
				String currentTabId = HttpTool.getParameter("currentTabId");
				assetinfo=(Assetinfo) assetinfoService.findById(id);
				HttpTool.getRequest().setAttribute("assetInfo", assetinfo);
				assetinfo.setState(assetDinfo.getState());
				assetDinfo.setAssetno(id);
				assetDinfo.setDeviceName(assetinfo.getName());
				assetDinfo.setCreateTime(new Date());
				
				AssetDamageInfo assetDamageInfo=(AssetDamageInfo)assetDinfoService.findMax();
				if(assetDinfoService.findMax()==null){
					assetDinfo.setDamageNo(Duuid.oddNumber(null));
				}else{
					assetDinfo.setDamageNo(Duuid.oddNumber(assetDamageInfo.getDamageNo()));
				}
					
				assetDinfoService.save(assetDinfo);
				assetDinfoService.update(assetinfo);
				HttpTool.getRequest().setAttribute("assetDInfo", assetDinfo);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
			} catch (Exception e) {
				json.accumulate("statusCode", 300);
				json.accumulate("message", "操作失败");
				e.printStackTrace();
			}
			renderJson(json);
			return null;
		}
	
		/**
		 * 修改前查询报损资产信息
		 * @return
		 */
		public String modifyAssetDinfo(){
			assetDinfo=(AssetDamageInfo) assetDinfoService.findById(id);
			String id=assetDinfo.getAssetno();
			assetinfo=(Assetinfo) assetinfoService.findById(id);
			return "modifyAssetDinfo";
		}
		
		/**
		 * 修改资产报损信息
		 * @return
		 */
		public String updateAssetDinfo(){
			JSONObject json = new JSONObject();
			try {
				
				assetDinfoService.update(assetDinfo);
				HttpTool.getRequest().setAttribute("assetDInfo", assetDinfo);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("callbackType", "closeCurrent");
			} catch (Exception e) {
				json.accumulate("statusCode", 300);
				json.accumulate("message", "操作失败");
				e.printStackTrace();
			}
			renderJson(json);
			return null;
		}
	
	public AssetDamageInfo getAssetDinfo() {
		return assetDinfo;
	}
	public void setAssetDinfo(AssetDamageInfo assetDinfo) {
		this.assetDinfo = assetDinfo;
	}
	public Assetinfo getAssetinfo() {
		return assetinfo;
	}
	public void setAssetinfo(Assetinfo assetinfo) {
		this.assetinfo = assetinfo;
	}
	

}
